﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;

/// <summary>
/// 网络消息的总调度
/// </summary>
public class EventManager {

    private static EventManager _instance;
    public static EventManager Instance
    {
        get
        {
            if(_instance==null)
            {
                _instance = new EventManager();
            }
            return _instance;
        }
    }
    public static System.Action HeartBeat;//心跳

    public static System.Action<UserData, bool> loginSuccess;//登录成功
    public static System.Action<MoveData> playerMove;//玩家已从
    public static System.Action<UserData> playerPosition;//老玩家更新
    public static System.Action<UserData> PlayerLogout;//玩家退出
    public static System.Action<FireData> PlayerFire;//玩家开火
    public static System.Action<HitDat> PlayerOnHit;//玩家受伤
    public static System.Action<UserData> PlayerOnDead;//玩家受伤
    public static System.Action<UserData> PlayerReady;//玩家准备
    public static System.Action<UserData> PlayerWin;//玩家胜利
    public static System.Action<List<UserData>> OtherPlayers;//其他玩家消息
    public static System.Action<List<UserData>> AllPlayerEnterGame;//全部准备，进入游戏

    public void OnMessage(ByteBuffer buffer)
    {
        //处理消息
        int protocol = buffer.ReadInt();
        string msg = buffer.ReadStringOnly();
        Debug.Log("协议" + protocol + ";消息体：" + msg);
        EventManager.HeartBeat();

        switch (protocol)
        {
            case Protocol.Login_CMD://别人登录成功
                Debug.Log("别人登录成功");
                UserData user = JsonUtility.FromJson<UserData>(msg);
                EventManager.loginSuccess(user, false);
                break;
            case Protocol.SelfLogin_CMD://自己登录成功
                Debug.Log("自己登录成功");
                NetworkManager.Instance.SetOnline(true);
                user = JsonUtility.FromJson<UserData>(msg);
                EventManager.loginSuccess(user, true);
                break;
            case Protocol.PlayerRepeatExit://用户重复登录
                Debug.LogWarning("用户重复登录");
                break;
            case Protocol.PlayerLogOut://有人退出
                Debug.Log("有人退出");

                UserData user1 = JsonUtility.FromJson<UserData>(msg);
                EventManager.PlayerLogout(user1);
                break;
            case Protocol.PlayerPosition_CMD:
                Debug.Log("老用户更新");
                UserData user2 = JsonUtility.FromJson<UserData>(msg);
                EventManager.playerPosition(user2);
                break;
            case Protocol.PlayerMove_CMD://玩家移动消息
                Debug.Log("玩家移动消息");
                MoveData data = JsonUtility.FromJson<MoveData>(msg);
                EventManager.playerMove(data);
                break;
            case Protocol.PlayerFire_CMD:
                Debug.Log("玩家开火");
                FireData fire = JsonUtility.FromJson<FireData>(msg);
                EventManager.PlayerFire(fire);
                break;
            case Protocol.PlayerOnHit_CMD:
                Debug.Log("玩家受伤");
                HitDat hit = JsonUtility.FromJson<HitDat>(msg);
                EventManager.PlayerOnHit(hit);
                break;
            case Protocol.PlayerDead_CMD:
                Debug.Log("玩家死亡");
                UserData dead = JsonUtility.FromJson<UserData>(msg);
                EventManager.PlayerOnDead(dead);
                break;
            case Protocol.HeartBeat:
                Debug.Log("<color=red>心跳</color>");
                EventManager.HeartBeat();
                break;
            case Protocol.PlayerReady_CMD:
                Debug.Log("玩家准备消息");
                UserData userInfo = JsonUtility.FromJson<UserData>(msg);
                EventManager.PlayerReady(userInfo);
                break;
            case Protocol.OtherPlayerInfo_CMD:
                Debug.Log("老玩家数据");
                //jsonmapper无法吧doubel强转为flaot ，接住jsonUtilty二次转化
                List<JsonData> datas = JsonMapper.ToObject<List<JsonData>>(msg);
                List<UserData> users = new List<UserData>();
                foreach (var item in datas)
                {
                    string json = JsonMapper.ToJson(item);
                    UserData udata = JsonUtility.FromJson<UserData>(json);
                    users.Add(udata);
                }
                EventManager.OtherPlayers(users);
                break;
            case Protocol.ReadyGoToGame_CMD:
                Debug.Log("全部准备，进入游戏");
                datas = JsonMapper.ToObject<List<JsonData>>(msg);
                users = new List<UserData>();
                foreach (var item in datas)
                {
                    string json = JsonMapper.ToJson(item);
                    UserData udata = JsonUtility.FromJson<UserData>(json);
                    users.Add(udata);
                }
                EventManager.AllPlayerEnterGame(users);
                break;
            case Protocol.PlayerWin_CMD:
                Debug.Log("剩余最后一名玩家胜利");
                user = JsonUtility.FromJson<UserData>(msg);
                //创建成功页面
                UIManager.Instance.GoPage("winPanel", user);
                break;
            case Protocol.Errer_CMD:
                Error error = JsonMapper.ToObject<Error>(msg);
                if(error.code==101)
                {
                    Debug.Log("101注册失败");
                }
                else if (error.code == 110)
                {
                    Debug.Log("110失败");
                }
                else if (error.code == 102)
                {
                    Debug.Log("登录失败");
                }
                break;
            case Protocol.UserRegister_CMD:
                Debug.Log("注册成功");
                break;
            case Protocol.UserLogin_CMD:
                Debug.Log("登录成功");
                break;
            default:
                break;
        }
    }
}
